package com.jz.community.moduleshopping.groupbuying.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.loaction.LocationManager;
import com.jz.community.basecomm.loaction.PoiManager;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.groupbuying.adapter.EditAddressAdapter;

import java.util.ArrayList;

import butterknife.BindView;

public class EditAddressActivity extends BaseMvpActivity {


    @BindView(R2.id.iv_edit_address_back)
    ImageView ivEditAddressBack;
    @BindView(R2.id.tv_edit_address_title)
    TextView tvEditAddressTitle;
    @BindView(R2.id.coupon_list_toolbar)
    Toolbar couponListToolbar;
    @BindView(R2.id.iv_edit_address_dingwei)
    ImageView ivEditAddressDingwei;
    @BindView(R2.id.tv_edit_address_dingwei)
    TextView tvEditAddressDingwei;
    @BindView(R2.id.rl_edit_address_dingwei)
    RelativeLayout rlEditAddressDingwei;
    @BindView(R2.id.iv_edit_address_sousuo)
    ImageView ivEditAddressSousuo;
    @BindView(R2.id.et_edit_address_sousuo)
    EditText etEditAddressSousuo;
    @BindView(R2.id.iv_edit_address_qingchu)
    ImageView ivEditAddressQingchu;
    @BindView(R2.id.rl_edit_address_sousuo)
    RelativeLayout rlEditAddressSousuo;
    @BindView(R2.id.tv_edit_address_quxiao)
    TextView tvEditAddressQuxiao;
    @BindView(R2.id.tv_edit_address_location)
    TextView tvEditAddressLocation;
    @BindView(R2.id.rv_edit_address_list)
    RecyclerView rvEditAddressList;

    EditAddressAdapter addressAdapter;
    View NullView;
    private SpannableString sStr;
    double lat = 0;
    double lon = 0;
    private String cityCode;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_edit_address;
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }


    @Override
    protected void initView() {
        setImmersionBar(couponListToolbar);
        NullView = LayoutInflater.from(this).inflate(R.layout.module_shopping_layout_edit_address_null, (ViewGroup) rvEditAddressList.getParent(), false);
        addressAdapter = new EditAddressAdapter(R.layout.module_shopping_item_edit_address, new ArrayList<>());
        rvEditAddressList.setLayoutManager(new LinearLayoutManager(this));
        rvEditAddressList.setAdapter(addressAdapter);
    }


    @Override
    protected void initDatas() {
        beginLocation();
    }

    @Override
    protected void addListener() {

        ivEditAddressBack.setOnClickListener(v -> finish());

        etEditAddressSousuo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString())) {
                    ivEditAddressQingchu.setVisibility(View.VISIBLE);
                    tvEditAddressQuxiao.setVisibility(View.VISIBLE);
                } else {
                    ivEditAddressQingchu.setVisibility(View.INVISIBLE);
                    tvEditAddressQuxiao.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        ivEditAddressQingchu.setOnClickListener(v -> etEditAddressSousuo.setText(""));

        tvEditAddressQuxiao.setOnClickListener(v -> finish());

        etEditAddressSousuo.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String keyWord = etEditAddressSousuo.getText().toString().trim();
                addressAdapter.setKeyword(keyWord);
                beginSearch(keyWord,cityCode);
            }
            return false;
        });

        addressAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putParcelable("address", addressAdapter.getData().get(position));
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    private void beginLocation() {
        LocationManager lm = new LocationManager();
        lm.getLocation(result -> {
            if (result.mResultId == 0) {
                lat = result.mLocation.mLatitude;
                lon = result.mLocation.mLongitude;
                cityCode = result.mLocation.mCityCode;
                EditAddressActivity.this.lnToCommunity(result.mLocation.mLatitude, result.mLocation.mLongitude, "", result.mLocation.mCityCode);
            } else {
                tvEditAddressDingwei.setText("定位失败");
            }
        }, this);
    }


    private void beginSearch(String keyWord,String cityCode) {
        new PoiManager(this, lat, lon, keyWord, false, list -> {
            if (!Preconditions.isNullOrEmpty(list)) {
                addressAdapter.setNewData(list);
            } else {
                addressAdapter.setEmptyView(NullView);
            }
        },cityCode);
    }


    private void lnToCommunity(final double lat, final double lon, String keyWord,String cityCode) {
        new PoiManager(this, lat, lon, keyWord, true, list -> {
            if (!Preconditions.isNullOrEmpty(list)) {
                tvEditAddressDingwei.setText(list.get(0).getCityName());
                sStr = new SpannableString("[当前定位] " + list.get(0).getProvinceName() + list.get(0).getCityName() + list.get(0).getAdName() + list.get(0).getSnippet() + list.get(0).getTitle());
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff9314"));
                sStr.setSpan(colorSpan, 0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tvEditAddressLocation.setText(sStr);
                tvEditAddressLocation.setOnClickListener(v -> {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("address", list.get(0));
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                });
            } else {
                tvEditAddressDingwei.setText("定位失败");
                tvEditAddressLocation.setText("定位失败");
            }
        },cityCode);
    }
}
